package mock;

import bean.City;
import com.github.javafaker.Faker;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

public class CityDataGenerator {
    private static final String DB_URL = "jdbc:mysql://192.168.10.102:3306/driver?useSSL=false&requireSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "000000";
    private static final int BATCH_SIZE = 100;

    public static void main(String[] args) {
        int count = 100; // 默认生成10条数据

        // 解析命令行参数
        if (args.length >= 1) {
            count = Integer.parseInt(args[0]);
        }

        generateData(count);
    }

    public static void generateData(int count) {
        Faker faker = new Faker(Locale.CHINA);
        List<City> cities = new ArrayList<>();

        for (long i = 1; i <= count; i++) {
            City city = City.builder()
                    .city_id(String.valueOf(i))
                    .province_name(generateRandomProvince(faker))
                    .city_name(generateRandomCity(faker))
                    .district_name(generateRandomDistrict(faker))
                    .street_name(generateRandomStreet(faker))
                    .build();
            cities.add(city);
        }

        // 插入数据库
        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            conn.setAutoCommit(false);
            String sql = "INSERT INTO city (" +
                    "city_id, province_name, city_name, district_name, street_name" +
                    ") VALUES (?, ?, ?, ?, ?)";

            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                for (int i = 0; i < cities.size(); i++) {
                    City city = cities.get(i);
                    pstmt.setString(1, city.getCity_id());
                    pstmt.setString(2, city.getProvince_name());
                    pstmt.setString(3, city.getCity_name());
                    pstmt.setString(4, city.getDistrict_name());
                    pstmt.setString(5, city.getStreet_name());
                    pstmt.addBatch();

                    if ((i + 1) % BATCH_SIZE == 0) {
                        pstmt.executeBatch();
                    }
                }
                pstmt.executeBatch();
                conn.commit();
                System.out.println("成功插入" + count + "条城市数据");
            } catch (SQLException e) {
                conn.rollback();
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 随机生成省份名称
     */
    public static String generateRandomProvince(Faker faker) {
        String[] provinces = {
                "北京市", "上海市", "天津市", "重庆市", "河北省", "河南省", "云南省", "辽宁省",
                "黑龙江省", "湖南省", "安徽省", "山东省", "新疆维吾尔自治区", "江苏省", "浙江省",
                "江西省", "湖北省", "广西壮族自治区", "甘肃省", "山西省", "内蒙古自治区", "陕西省",
                "吉林省", "福建省", "贵州省", "广东省", "青海省", "西藏自治区", "海南省"
        };
        return provinces[faker.random().nextInt(provinces.length)];
    }

    /**
     * 随机生成城市名称（根据省份可扩展）
     */
    public static String generateRandomCity(Faker faker) {
        String[] cities = {
                "北京市", "上海市", "广州市", "深圳市", "成都市", "武汉市", "西安市", "杭州市",
                "南京市", "济南市", "郑州市", "长沙市", "哈尔滨市", "长春市", "沈阳市", "昆明市",
                "贵阳市", "南宁市", "乌鲁木齐市", "拉萨市", "西宁市", "银川市", "兰州市", "太原市",
                "合肥市", "南昌市", "海口市", "三亚市", "青岛市", "大连市", "宁波市", "厦门市"
        };
        return cities[faker.random().nextInt(cities.length)];
    }

    /**
     * 随机生成区名
     */
    public static String generateRandomDistrict(Faker faker) {
        String[] districts = {
                "朝阳区", "海淀区", "浦东新区", "天河区", "武侯区", "西湖区", "鼓楼区", "江岸区",
                "碑林区", "雁塔区", "南山区", "福田区", "锦江区", "青羊区", "渝中区", "江北区",
                "东城区", "西城区", "静安区", "黄浦区", "徐汇区", "长宁区", "虹口区", "普陀区"
        };
        return districts[faker.random().nextInt(districts.length)];
    }

    /**
     * 随机生成街道名称
     */
    public static String generateRandomStreet(Faker faker) {
        String[] streets = {
                "中山路", "解放路", "人民路", "建设路", "和平路", "文化路", "新华路", "东风路",
                "朝阳路", "长安街", "南京路", "淮海路", "延安路", "滨江大道", "科技大道", "世纪大道"
        };
        return streets[faker.random().nextInt(streets.length)] + " " +
                faker.address().streetAddressNumber();
    }
}
